<?php
namespace app\weixin\controller;

use think\Db;
use think\Request;
use think\Controller;

class Index extends Controller
{
    public function index(Request $request)
    {
        $appid = 'wx30ccb03ca1f39ddf';
        $appsecret = '23d6672db6d518db2d926bf9b65a350f';

        if(!empty($_SESSION['openid']) && !empty($_SESSION['nickname'])){
            $result = Db::table('shangchang')->where('openid',$_SESSION['openid'])->find();

            if($result){

                $gift = trim($result['prize']);

                if($gift == '猫耳耳机'){
                    $desc = 'headset';
                }elseif($gift == '巨型公仔'){
                    $desc = 'doll';
                }elseif($gift == '毛绒玩具'){
                    $desc = 'toy';
                }elseif($gift == '100元现金红包'){
                    $desc = 'money';
                }elseif($gift == '花样抱枕'){
                    $desc = 'bolster';
                }elseif($gift == '精品餐具'){
                    $desc = 'tableware';
                }
                $result['desc'] = $desc;
                $this->assign('user_info',$result);
                return $this->fetch('index');
//                return json($result);
            }else{
                return $this->fetch('index');
            }
        }


        $code = $request->param('code');

        if(empty($code)) $this->go_oauth($appid);

        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

        $token = $this->https_request($url);

        if(strpos($token, 'access_token') !== false){
            $res = json_decode($token,true);
            //获取用户信息
            $result = Db::table('shangchang')->where('openid',$res['openid'])->find();

            if($result){

                $gift = trim($result['prize']);

                if($gift == '猫耳耳机'){
                    $desc = 'headset';
                }elseif($gift == '巨型公仔'){
                    $desc = 'doll';
                }elseif($gift == '毛绒玩具'){
                    $desc = 'toy';
                }elseif($gift == '100元现金红包'){
                    $desc = 'money';
                }elseif($gift == '花样抱枕'){
                    $desc = 'bolster';
                }elseif($gift == '精品餐具'){
                    $desc = 'tableware';
                }
                $result['desc'] = $desc;
                $this->assign('user_info',$result);
                return $this->fetch('index');
//                return json($result);
            }


            $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$res['access_token'].'&openid='.$res['openid'].'&lang=zh_CN';
            $user_info = $this->https_request($url);
            if(strpos($user_info, 'openid') !== false){
                $user_info = json_decode($user_info,true);
                $_SESSION = $user_info;
                return $this->fetch('index');
            }else{
                $this->go_oauth($appid);
            }

        }else{
            $this->go_oauth($appid);
        }

    }

    public function ajax(Request $request){
        $code = $request->param('code');
//        var_export($_SESSION);
        if(!isset($_SESSION['openid']) || !isset($_SESSION['nickname']) || empty($code)){
            return json(['code'=>-400,'msg'=>'非法操作']);
        }

        if($res = Db::table('shangchang')->where('openid',$_SESSION['openid'])->find()){
            return json(['code'=>-403,'msg'=>'该微信已领过']);
        }

        $result = Db::table('shangchang')->where('code',$code)->find();
//        var_dump($result);die;
        if(!$result){
            return json(['code'=>-401,'msg'=>'密码错误']);
        }

        if(!empty($result['openid'])){
            return json(['code'=>402,'msg'=>'密码已使用']);
        }

        $gift = trim($result['prize']);

        if($gift == '猫耳耳机'){
            $prizeId = 0;
        }elseif($gift == '巨型公仔'){
            $prizeId = 1;
        }elseif($gift == '毛绒玩具'){
            $prizeId = 2;
        }elseif($gift == '100元现金红包'){
            $prizeId = 3;
        }elseif($gift == '花样抱枕'){
            $prizeId = 4;
        }elseif($gift == '精品餐具'){
            $prizeId = 5;
        }

        $r = Db::table('shangchang')
            ->where('code', $code)
            ->update(['nickname' => $_SESSION['nickname'],'openid'=>$_SESSION['openid']]);
        if($r){
//            return json(['code'=>200,'gift'=>$result['prize']]);
            return json(['code'=>200,'msg'=>'成功领取礼物','result'=>['prizeId'=>$prizeId,'gift'=>$gift]]);
        }


    }

    public function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public function go_oauth($appid){
        $redirect_url = 'http://1737u974l9.51mypc.cn/weixin/index/index';
        $redirect_url = urlencode($redirect_url);
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';

        header('Location:'.$url);die;
    }
}
